//
//  NRWCarAddIntentionVC.m
//  New_Runway
//
//  Created by NewLife on 2018/2/28.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCarAddIntentionVC.h"
#import "NRWCarInputItemView.h"
#import "CustomerDetailCell.h"
#import "CRMSearchShopCarListVC.h"
#import "NRWCarListsModel.h"
#import "NRWCarChooseTableVC.h"
#import "NRWCarRemarkTextView.h"

@interface NRWCarAddIntentionVC ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, CustomerDetailCellDelegate>

@property (strong, nonatomic) NSArray <Result *>*selecCarModelArr;

@property (strong, nonatomic) NSArray <NSString *>*selectCarIdArrM;

@end

NSInteger fromItemTag = 888;
NSInteger chooseCarItemTag = 889;

@implementation NRWCarAddIntentionVC
{
    UITableView *tableView;
    NRWCarRemarkTextView *remarkTextView;
    
    NRWCarInputItemView *fromItem;
    
    NSString *fromId;
}

#pragma mark - lifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = @"添加意向";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupUI];
    
    [self addTableViewHeaderView];
    
    [self addRightItem];
}

#pragma mark - setupUI
- (void)addRightItem {
    
    [self.navigationBarView setupRightItemTitle:@"保存"];
}

- (void)setupUI {
    
    tableView = DPTableViewForStyle(UITableViewStylePlain)
    .setFrame(CGRectMake(0, self.navH, Screen_width, Screen_height - self.navH - self.BotH))
    .setTableFooterView(DPView())
    .registerCell([CustomerDetailCell class], @"cell");
    
    tableView.delegate = self;
    tableView.dataSource = self;
    
    self.view.addSubview(tableView);
}

- (void)addTableViewHeaderView {
    
    UIView *headerView = DPView().setBackgroundColor([UIColor whiteColor]);
    
    fromItem = [[NRWCarInputItemView alloc] initWithInputItemWithPlaceHolder:@"请选择客户来源" delegate:self showArrow:YES];
    fromItem.frame = CGRectMake(0, 0, Screen_width, 40);
    fromItem.titleString = @"客户来源";
    fromItem.tag = fromItemTag;
    
    NRWCarInputItemView *chooseCar = [[NRWCarInputItemView alloc] initWithInputItemWithPlaceHolder:@"请选择意向车辆" delegate:self showArrow:YES];
    chooseCar.frame = CGRectMake(0, fromItem.bottom, Screen_width, 40);
    chooseCar.titleString = @"车辆选择";
    chooseCar.tag = chooseCarItemTag;
    
    UILabel *remarkLabel = DPLabel()
    .setFont(kPingFangRegular13())
    .setTextColor(getColorFromString(@"323232"))
    .setText(@"备注");
    
    remarkTextView = [[NRWCarRemarkTextView alloc] initWithRemarkTextView];
    
    headerView
    .addSubview(remarkLabel)
    .addSubview(remarkTextView)
    .addSubview(fromItem)
    .addSubview(chooseCar);
    
    [remarkLabel sizeToFit];
    remarkLabel.frame = CGRectMake(16, chooseCar.bottom + 11, remarkLabel.width, remarkLabel.height);
    remarkTextView.frame = CGRectMake(16, remarkLabel.bottom + 11, Screen_width - 32, 100);
    
    headerView.frame = CGRectMake(0, 0, Screen_width, 225);
    tableView.tableHeaderView = headerView;
}

#pragma mark - privateMethod
- (void)rightItemClick {
    
    if (self.selecCarModelArr.count == 0) {
        [SVProgressHUD showWithStatus:@"车辆不能为空" dismissDelay:1];
        return;
    }
    
    NSString *channelId = fromId;
    NSString *channelName = fromItem.inputText;
    NSString *customerShopId = self.customerShopId;
    NSString *intentionRemark = remarkTextView.text;
    NSString *intentionType = @"00";
    
    NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.selecCarModelArr.count];
    for (Result *r in self.selecCarModelArr) {
     
        [arrM addObject:[r mj_JSONObject]];
    }
    
    NSString *modelList = [arrM mj_JSONString];
    
    NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
    
    if (!isEmptyOrNilString(channelId)) {
        [dictM setObject:channelId forKey:@"channelId"];
    }
    
    if (!isEmptyOrNilString(channelName)) {
        [dictM setObject:channelName forKey:@"channelName"];
    }
    
    if (!isEmptyOrNilString(modelList)) {
        [dictM setObject:modelList forKey:@"modelList"];
    }
    
    if (!isEmptyOrNilString(intentionType)) {
        [dictM setObject:intentionType forKey:@"intentionType"];
    }
    
    if (!isEmptyOrNilString(customerShopId)) {
        [dictM setObject:customerShopId forKey:@"customerShopId"];
    }
    if (!isEmptyOrNilString(intentionRemark)) {
        [dictM setObject:intentionRemark forKey:@"intentionRemark"];
    }
    [SVProgressHUD show];
    [YQNetworking postWithUrl:[NSString stringWithFormat:@"%@%@",CRMContextApi, addIntentionAPI] refreshRequest:NO cache:NO params:dictM.copy progressBlock:nil successBlock:^(id response) {
        
        [SVProgressHUD dismiss];
        if ([response[@"code"] integerValue] == 1) {
            
            [SVProgressHUD showSuccessWithStatus:@"添加成功" dismissDelay:1 didDismiss:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        } else {
            [SVProgressHUD showErrorWithStatus:response[@"msg"] dismissDelay:1];
        }
    } failBlock:^(NSError *error) {
        [ShowMessage showHintMessage:@"请求失败!" duration:2.0 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
}

#pragma mark - publicMethod

#pragma mark - DelegateMethod
#pragma mark TableViewDelegate & dataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.selecCarModelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomerDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    Result *resultModel = _selecCarModelArr[indexPath.row];
    
    cell.imageUrlStr = [NSString stringWithFormat:@"%@%@",ImageApi, resultModel.carImageUrl];
    cell.carNameStr = resultModel.carTitle;
    cell.carPriceStr = [NSString stringWithFormat:@"¥%.2f万", resultModel.carRetailPrice];
    cell.tag = indexPath.row;
    cell.showDeleteBtn = YES;
    cell.delegate = self;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
    view.opaque = YES;
    
    CALayer *layer = [CALayer layer];
    layer.backgroundColor = [UIColor colorFromHexCode:@"ff6816"].CGColor;
    layer.frame = CGRectMake(0, 0, 2, 25);
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"买车意向";
    label.textColor = [UIColor colorFromHexCode:@"323232"];
    label.font = kPingFangRegular13();
    label.frame = CGRectMake(8, 0, 100, 25);
    
    [view addSubview:label];
    [view.layer addSublayer:layer];
    
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 25;
}

#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    if (textField.tag == fromItemTag) {
        
        NRWCarChooseTableVC *vc = [[NRWCarChooseTableVC alloc] init];
        vc.chooseType = NRWCarChooseTableVCTypeChannel;
        vc.fromClass = self.class;
        [self.navigationController pushViewController:vc animated:YES];
        
        vc.chooseCompleted = ^(NSString *itemId, NSString *itemName) {
            
            fromId = itemId;
            fromItem.inputText = itemName;
        };
        
    } else if (textField.tag == chooseCarItemTag) {
        
        CRMSearchShopCarListVC *vc = [[CRMSearchShopCarListVC alloc] init];
        vc.selectCarModelArrM = self.selecCarModelArr.mutableCopy;
        vc.selectCarIdArrM = self.selectCarIdArrM.mutableCopy;
        [self.navigationController pushViewController:vc animated:YES];
    }
    return NO;
}

#pragma mark CustomerDetailCellDelegate
- (void)customerDetailCell:(CustomerDetailCell *)cell deleteBtnClickWithIndex:(NSInteger)index {
    
    NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.selecCarModelArr];
    [arrM removeObjectAtIndex:index];
    _selecCarModelArr = arrM.copy;
    
    NSMutableArray *arrM2 = [NSMutableArray arrayWithArray:self.selectCarIdArrM];
    [arrM2 removeObjectAtIndex:index];
    _selectCarIdArrM = arrM2.copy;
    
    [tableView reloadData];
}

#pragma mark - touchEventAction

#pragma mark - setter
- (void)setSelecCarModelArr:(NSArray<Result *> *)selecCarModelArr {
    
    _selecCarModelArr = selecCarModelArr;
    
    [tableView reloadData];
}

#pragma mark - getter



@end
